home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / dstart.s < prev    next >
Text File  |  1990-11-23  |  7KB  |  265 lines

  1. *************************************************************************
  2. *                                    *
  3. *    DSTART.S    Startup module for C programs using dLibs    *
  4. *                                    *
  5. *************************************************************************
  6.  
  7. *
  8. * entry points
  9. *
  10. .globl    __base            * basepage pointer
  11. .globl    __start            * startup entry point
  12. .globl    _etext            * end of text segment
  13. .globl    _edata            * end of data segment
  14. .globl    _end            * end of BSS segment (end of program)
  15. .globl    __break            * location of stack/heap break
  16. .globl    __exit            * terminate immediately with exit code
  17. .globl    __argc            * number of arguments
  18. .globl    __argv            * argument list pointer
  19. .globl    __envp            * environment string pointer
  20. .globl    _errno            * system error number
  21. .globl    _gemdos            * trap #1 (gemdos) hook
  22. .globl    _bios            * trap #13 (bios) hook
  23. .globl    _xbios            * trap #14 (xbios) hook
  24. .globl    _bdos            * trap #2 (bdos) hook
  25.  
  26. *
  27. * external references
  28. *
  29. .globl    __STKSIZ        * Stack size value from C (unsigned long)
  30. .globl    __main            * Initial entry point for C program
  31. .globl    _main            * C program main() function
  32.  
  33. *
  34. * useful constants
  35. *
  36. MINSTK        equ    1024    * Minimum 1K stack size
  37. MARGIN        equ    512    * Minimum memory to return to OS
  38.  
  39. *
  40. * GEMDOS functions
  41. *
  42. Cconws        equ    $09    * Console write string
  43. Pterm        equ    $4C    * Process terminate (with exit code)
  44. Mshrink        equ    $4A    * Shrink program space
  45.  
  46. *
  47. * basepage offsets
  48. *
  49. p_hitpa        equ    $04    * top of TPA
  50. p_tbase        equ    $08    * base of text
  51. p_tlen        equ    $0C    * length of text
  52. p_dbase        equ    $10    * base of data
  53. p_dlen        equ    $14    * length of data
  54. p_bbase        equ    $18    * base of BSS
  55. p_blen        equ    $1C    * length of BSS
  56. p_env        equ    $2C    * environment string
  57. p_cmdlin    equ    $80    * command line image
  58.  
  59. *
  60. * STARTUP ROUTINE (must be first object in link)
  61. *
  62. .text
  63. __start:
  64. *
  65. * save initial stack and basepage pointers
  66. *
  67.     move.l    sp,a5            * a5 = initial stack pointer
  68.     move.l    4(sp),a4        * a4 = basepage address
  69.     move.l    a4,__base
  70.     move.l    p_tbase(a4),d3
  71.     add.l    p_tlen(a4),d3
  72.     move.l    d3,_etext        * end of text segment
  73.     move.l    p_dbase(a4),d3
  74.     add.l    p_dlen(a4),d3
  75.     move.l    d3,_edata        * end of data segment
  76.     move.l    p_bbase(a4),d3
  77.     add.l    p_blen(a4),d3
  78.     move.l    d3,_end            * end of BSS (end of program)
  79.     move.l    d3,__break;        * set initial _break value
  80.     move.l    p_env(a4),__envp    * save environment pointer
  81. *
  82. * call C function to get command line arguments
  83. *
  84.     lea.l    p_cmdlin(a4),a0        * get pointer to command line image
  85.     move.b    (a0)+,d0
  86.     ext.w    d0            * extract length
  87.     move.w    d0,-(sp)        * cmdlen
  88.     move.l    a0,-(sp)        * cmdline
  89.     jsr    __initar        * call _initargs(cmdline, cmdlen)
  90.     addq.l    #6,sp
  91. *
  92. * calculate free space available to program
  93. *
  94.     move.l    __break,d3
  95.     move.l    d3,a3            * a3 = base of free space
  96.     neg.l    d3
  97.     add.l    p_hitpa(a4),d3
  98.     sub.l    #MARGIN,d3        * d3 = free space
  99. *
  100. * calculate new stack size (store in d2)
  101. *
  102.     move.l    #__STKSIZ,a2        * a2 = &_STKSIZ
  103.     move.l    a2,d2            * if __STKSIZ is undefined
  104.     beq    minimum            *   use MINSTK
  105.     move.l    (a2),d2            * if __STKSIZ is positive
  106.     bpl    setstk            *   use __STKSIZ
  107.     add.l    d3,d2            * if __STKSIZ is negative
  108.     cmp.l    #MINSTK,d2        *   try (free space + __STKSIZ)
  109.     bge    setstk            * if < MINSTK
  110. minimum:
  111.     move.l    #MINSTK,d2        *   use MINSTK
  112. *
  113. * check to see if there is enough room for requested stack
  114. *
  115. setstk:
  116.     cmp.l    d3,d2
  117.     blt    shrink            * if (available < requested)
  118.     move.l    #stkerr,-(sp)
  119.     move.w    #Cconws,-(sp)
  120.     trap    #1            *   report a stack error
  121.     addq.l    #6,sp
  122.     move.w    #-39,-(sp)
  123.     move.w    #Pterm,-(sp)
  124.     trap    #1            *   and return error -39 (ENSMEM)
  125. *
  126. * set up new stack pointer and Mshrink
  127. *
  128. shrink:
  129.     add.l    a3,d2            * new stack = free base + stack size
  130.     move.l    d2,sp
  131.     sub.l    a4,d2            * keep space = new stack - __base
  132.     move.l    d2,-(sp)
  133.     move.l    a4,-(sp)
  134.     clr.w    -(sp)
  135.     move.w    #Mshrink,-(sp)
  136.     trap    #1            * Mshrink(0, _base, keep);
  137.     add.l    #12,sp
  138. *
  139. * call C entry point function _main()
  140. *
  141.     jsr    __main            * if _main returns
  142.     move.w    d0,4(sp)        *   insert return value and fall thru
  143.  
  144. *
  145. * void _exit(code)
  146. *   int code;
  147. *
  148. * Terminate process with a return value of <code>
  149. *
  150. __exit:
  151.     tst.l    (sp)+            * pop return PC off the stack
  152.     move.w    #Pterm,-(sp)        *   leaving <code>
  153.     trap    #1            *   and terminate.
  154.  
  155. *
  156. * operating system trap hooks for C
  157. *
  158.  
  159. *
  160. * long gemdos(function, [parameter, ...])
  161. *   int function;
  162. *
  163. _gemdos:
  164.     move.l    (sp)+,traprtn        * save return address
  165.     trap    #1            * do gemdos trap
  166.     bra    chkstk
  167.  
  168. *
  169. * long bios(function, [parameter, ...])
  170. *   int function;
  171. *
  172. _bios:
  173.     move.l    (sp)+,traprtn        * save return address
  174.     trap    #13            * do bios trap
  175.     bra    chkstk
  176.  
  177. *
  178. * long xbios(function, [parameter, ...])
  179. *   int function;
  180. *
  181. _xbios:
  182.     move.l    (sp)+,traprtn        * save return address
  183.     trap    #14            * do xbios trap
  184.     bra    chkstk
  185.  
  186. *
  187. * int bdos(function, parameter)
  188. *   int function;
  189. *   long parameter;
  190. *
  191. _bdos:
  192.     move.l    (sp),traprtn        * save return address
  193.     move.l    a6,(sp)            * save old frame pointer
  194.     move.l    sp,a6            * set up frame pointer
  195.     tst.l    -(a6)            *   (fake "link a6,#0")
  196.     move.w    8(a6),d0        * function code in D0.W
  197.     move.l    10(a6),d1        * parameter value in D1.L
  198.     trap    #2            * do bdos trap
  199.     move.l    (sp)+,a6        * restore old frame pointer
  200.  
  201. *
  202. * check for stack overflow (done after all OS traps)
  203. *
  204. chkstk:
  205.     cmp.l    __break,sp
  206.     bgt    nosweat            * if (_break > sp)
  207.     move.l    #stkovf,-(sp)
  208.     move.w    #Cconws,-(sp)
  209.     trap    #1            *   report a stack overflow
  210.     addq.l    #6,sp
  211.     move.w    #-1,-(sp)
  212.     move.w    #Pterm,-(sp)
  213.     trap    #1            *   and return error -1 (ERROR)
  214. nosweat:
  215.     move.l    traprtn,-(sp)        * else, restore return address
  216.     rts                * and do a normal return.
  217.  
  218. *
  219. * this call to _main ensures that it the user's main() function will be
  220. * linked, even if it is in a library.
  221. *
  222.     jsr    _main            * NO PATH TO THIS STATEMENT
  223.  
  224. * THE FOLLOWING IS SELF MODIFYING CODE, DO NOT DISTURB
  225.     move.l    #$644c6962,$7320(sp)
  226.     moveq.l    #$31,d3
  227.     move.l    $0(a2,d0),d7
  228.  
  229. *
  230. * initialized data space
  231. *
  232. .data
  233. .even
  234. stkerr:                    * not enough memory for stack
  235.     .dc.b    'Not enough memory',$d,$a,0
  236. stkovf:                    * impending stack overflow
  237.     .dc.b    'Stack overflow',$d,$a,0
  238. _errno:                    * system error number
  239.     .dc.w    0
  240. __argc:                    * number of command line args
  241.     .dc.w    0
  242. __argv:                    * pointer to command line arg list
  243.     .dc.l    0
  244. *
  245. * uninitialized data space
  246. *
  247. .bss
  248. .even
  249. __base:                    * pointer to basepage
  250.     .ds.l    1
  251. _etext:                    * pointer to end of text segment
  252.     .ds.l    1
  253. _edata:                    * pointer to end of data segment
  254.     .ds.l    1
  255. _end:                    * pointer to end of BSS (end of program)
  256.     .ds.l    1
  257. __break:                * pointer to stack/heap break
  258.     .ds.l    1
  259. __envp:                    * pointer to environment string
  260.     .ds.l    1
  261. traprtn:                * storage for return PC in trap hooks
  262.     .ds.l    1
  263.  
  264. .end
  265.